package com.elsa.smart_buffet.service.impl;

import com.elsa.smart_buffet.mapper.CategoryPriceMapper;
import com.elsa.smart_buffet.pojo.ResultBox.CallBackMessage;
import com.elsa.smart_buffet.pojo.ResultBox.ResponseResult;
import com.elsa.smart_buffet.pojo.po.CategoryPrice;
import com.elsa.smart_buffet.pojo.vo.CategoryPriceStats;
import com.elsa.smart_buffet.service.CategoryPriceService;
import com.elsa.smart_buffet.utils.CategoryUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName : CategoryPriceServiceImpl  //类名
 * @Author : elsa //作者
 */
@Service
@Slf4j
@Transactional
public class CategoryPriceServiceImpl implements CategoryPriceService {

    @Autowired
    private CategoryPriceMapper categoryPriceMapper;

    @Autowired
    private CategoryUtil categoryUtil;

    @Override
    public ResponseResult getCategoryPriceByTime(String data) {
        log.info(String.valueOf(categoryPriceMapper.getCategoryPriceByTime(data)));
        List<CategoryPrice> list = categoryPriceMapper.getCategoryPriceByTime(data);
        List<CategoryPriceStats> categoryPriceStatsList = new ArrayList<>();
        int totalPrice = 0;
        int totalNum = 0;
        CategoryPriceStats categoryPriceStats1 = null;
        CategoryPriceStats categoryPriceStats2 = null;
        CategoryPriceStats categoryPriceStats3 = null;
        CategoryPriceStats categoryPriceStats4 = null;
        for (int i = 0; i < list.size(); i ++) {
            if(Objects.equals(list.get(i).getCategory(), "1")) {
                if(categoryPriceStats1 == null) {
                    categoryPriceStats1 = new CategoryPriceStats();
                    categoryPriceStats1.setCategory(categoryUtil.getCategory(list.get(i).getCategory()));
                    categoryPriceStats1.setNum(list.get(i).getNum());
                    totalNum += list.get(i).getNum();
                    categoryPriceStats1.setTotalPrice(list.get(i).getPrice());
                    totalPrice += list.get(i).getPrice();
                } else {
                    categoryPriceStats1.setNum(categoryPriceStats1.getNum() + list.get(i).getNum());
                    totalNum += list.get(i).getNum();
                    categoryPriceStats1.setTotalPrice(categoryPriceStats1.getTotalPrice() + list.get(i).getPrice());
                    totalPrice += list.get(i).getPrice();
                }
            } else if(Objects.equals(list.get(i).getCategory(), "2")) {
                if(categoryPriceStats2 == null) {
                    categoryPriceStats2 = new CategoryPriceStats();
                    categoryPriceStats2.setCategory(categoryUtil.getCategory(list.get(i).getCategory()));
                    categoryPriceStats2.setNum(list.get(i).getNum());
                    totalNum += list.get(i).getNum();
                    categoryPriceStats2.setTotalPrice(list.get(i).getPrice());
                    totalPrice += list.get(i).getPrice();
                } else {
                    categoryPriceStats2.setNum(categoryPriceStats2.getNum() + list.get(i).getNum());
                    totalNum += list.get(i).getNum();
                    categoryPriceStats2.setTotalPrice(categoryPriceStats2.getTotalPrice() + list.get(i).getPrice());
                    totalPrice += list.get(i).getPrice();
                }
            } else if(Objects.equals(list.get(i).getCategory(), "3")) {
                if(categoryPriceStats3 == null) {
                    categoryPriceStats3 = new CategoryPriceStats();
                    categoryPriceStats3.setCategory(categoryUtil.getCategory(list.get(i).getCategory()));
                    categoryPriceStats3.setNum(list.get(i).getNum());
                    totalNum += list.get(i).getNum();
                    categoryPriceStats3.setTotalPrice(list.get(i).getPrice());
                    totalPrice += list.get(i).getPrice();
                } else {
                    categoryPriceStats3.setNum(categoryPriceStats3.getNum() + list.get(i).getNum());
                    totalNum += list.get(i).getNum();
                    categoryPriceStats3.setTotalPrice(categoryPriceStats3.getTotalPrice() + list.get(i).getPrice());
                    totalPrice += list.get(i).getPrice();
                }
            } else {
                if(categoryPriceStats4 == null) {
                    categoryPriceStats4 = new CategoryPriceStats();
                    categoryPriceStats4.setCategory(categoryUtil.getCategory(list.get(i).getCategory()));
                    categoryPriceStats4.setNum(list.get(i).getNum());
                    totalNum += list.get(i).getNum();
                    categoryPriceStats4.setTotalPrice(list.get(i).getPrice());
                    totalPrice += list.get(i).getPrice();
                } else {
                    categoryPriceStats4.setNum(categoryPriceStats4.getNum() + list.get(i).getNum());
                    totalNum += list.get(i).getNum();
                    categoryPriceStats4.setTotalPrice(categoryPriceStats4.getTotalPrice() + list.get(i).getPrice());
                    totalPrice += list.get(i).getPrice();
                }
            }
        }
        if(categoryPriceStats1 == null) {
            categoryPriceStats1 = new CategoryPriceStats();
            categoryPriceStats1.setCategory(categoryUtil.getCategory("1"));
        }
        if(categoryPriceStats2 == null) {
            categoryPriceStats2 = new CategoryPriceStats();
            categoryPriceStats2.setCategory(categoryUtil.getCategory("2"));
        }
        if(categoryPriceStats3 == null) {
            categoryPriceStats3 = new CategoryPriceStats();
            categoryPriceStats3.setCategory(categoryUtil.getCategory("3"));
        }
        if(categoryPriceStats4 == null) {
            categoryPriceStats4 = new CategoryPriceStats();
            categoryPriceStats4.setCategory(categoryUtil.getCategory("4"));
        }
        categoryPriceStatsList.add(categoryPriceStats1);
        categoryPriceStatsList.add(categoryPriceStats2);
        categoryPriceStatsList.add(categoryPriceStats3);
        categoryPriceStatsList.add(categoryPriceStats4);

        for (int i = 0; i < categoryPriceStatsList.size(); i ++) {
            categoryPriceStatsList.get(i).setRatio(String.valueOf(Math.round((double) categoryPriceStatsList.get(i).getNum() / totalNum * 10000) / 100) + '%');
            categoryPriceStatsList.get(i).setTotalPriceRatio(String.valueOf(Math.round((double) categoryPriceStatsList.get(i).getTotalPrice() / totalPrice * 10000) / 100) + '%');
        }
        log.info(String.valueOf(totalPrice));
        log.info(String.valueOf(totalNum));
        log.info(String.valueOf(categoryPriceStatsList));
        return new ResponseResult(CallBackMessage.SUCCESS, categoryPriceStatsList);
    }

}
